

//
//  AxcRedEnvelopeView.m
//  hongbaoyu
//
//  Created by Axc_5324 on 16/12/27.
//  Copyright © 2016年 Axc_5324. All rights reserved.
//

#import "AxcRedEnvelopeView.h"



@interface AxcRedEnvelopeView ()

@property(nonatomic,assign)CGFloat self_Height;
@property(nonatomic,assign)CGFloat self_Width;

@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)NSTimer *cleanUpTimer;

@property(nonatomic,assign)NSInteger envelope_Count;

@end


@implementation AxcRedEnvelopeView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _self_Height            = frame.size.height;
        _self_Width             = frame.size.width;
        _AxcRedEnvelope_Count   = 100;  // 初始化默认红包数量
        _envelope_Count         = 0;   // 内部声明计数
        _landing_Time           = 2;  // 降落时间
        _AxcRedEnvelope_Size    = CGSizeMake(50, 50);   // 初始化默认大小
        _interval_Time          = 0.1;   // 初始化间隔
    }
    return self;
}



- (void)theRains{
    
    
    AxcRedEnvelope *btn;
    if ([_dataSource respondsToSelector:@selector(AxcRedEnvelope:createAtIndex:)]) {
        btn = [_dataSource AxcRedEnvelope:btn createAtIndex:_envelope_Count];
        btn.frame = [self getFrame];
    }else{
        btn = [[AxcRedEnvelope alloc] initWithFrame:[self getFrame]];
        if (!_AxcRedEnvelope_Image) {
            btn.backgroundColor = [UIColor redColor];
        }else{
            [btn setBackgroundImage:_AxcRedEnvelope_Image
                           forState:UIControlStateNormal];
        }
    }
    btn.tag = _envelope_Count;
    [btn addTarget:self
            action:@selector(clickAxcRedEnvelopeBtn:)
  forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn];
    
    [UIView animateWithDuration:_landing_Time
                     animations:^{
                         btn.frame = CGRectMake(btn.frame.origin.x,
                                                self.frame.size.height +100,
                                                _AxcRedEnvelope_Size.width,
                                                _AxcRedEnvelope_Size.height);
                     }completion:^(BOOL finished) {
                         [btn removeFromSuperview]; // 移除废弃红包
                     }];
    _envelope_Count ++;
}

- (CGRect )getFrame{
    NSInteger X = arc4random()%(NSInteger)self.frame.size.width - _AxcRedEnvelope_Size.width;
    return CGRectMake(X < 0 ? 0:X,
                      - _AxcRedEnvelope_Size.height - 20,
                      _AxcRedEnvelope_Size.width,
                       _AxcRedEnvelope_Size.height);
}


- (void)clickAxcRedEnvelopeBtn:(AxcRedEnvelope *)sender{
    if ([_delegate respondsToSelector:@selector(AxcRedEnvelope:didSelectAtIndex:)]) {
        [_delegate AxcRedEnvelope:sender didSelectAtIndex:sender.tag];
    }
}

- (void)starPerformance{
    _timer = [NSTimer scheduledTimerWithTimeInterval:_interval_Time
                                              target:self
                                            selector:@selector(theRains)
                                            userInfo:nil
                                             repeats:YES];
}

-(void)stopPerformance{
    [_timer invalidate];
    _timer = nil; // 置空保护loop线程
}




@end
